package com.liang.security.config;

import com.liang.security.authentication.AuthenticationSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

/**
 * @Author: liangjy
 * @Date:2022/2/5 20:33
 * @Package_name: com.liang.security.config
 * @Description: Security的配置
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;
    //密码编辑器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     *
     * @param http
     * @throws Exception
     * @Description: security的安全拦截机制
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()// 禁用csrf
                .authorizeRequests()
                .antMatchers("/r/r1").hasAuthority("p1")//拥有p1权限的人才能访问r1资源
                .antMatchers("/r/r2").hasAuthority("p2")//拥有p2权限的人才能访问r2资源
                .antMatchers("/z/z1").hasAuthority("p3")//拥有p2权限的人才能访问r2资源
                .antMatchers("/r/**").authenticated()//对r/**的资源需要认证
                .antMatchers("/z/**").authenticated()//对z/**的资源需要认证
                .antMatchers("/login","authentication/require"
                        ,"/authentication/form").permitAll()//对以上资源路径放行
                .anyRequest()//任何请求
                .authenticated()//都需要身份验证
                .and()
                .formLogin()//允许表单登录
                .loginPage("/login")//登录页 默认重定向
                .loginProcessingUrl("/authentication/form")//自定义登录跟前端绑定的
                .successForwardUrl("/login-success")//登录成功后跳转页
                .successHandler(authenticationSuccessHandler)//自定义成功鉴权
                .failureHandler(authenticationFailureHandler)//自定义失败鉴权
                .and()
                .logout()
                .logoutUrl("/logout");
    }
}
